package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 执行关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        try(Connection connection =DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            /*
            SELECT t.name,c.name,c.floor
            FROM teacher t
            JOIN class c ON t.id = c.teacher_id
            WHERE t.name='王克晶'
             */
            String sql = "SELECT t.name,c.name,c.floor\n" +
                    "            FROM teacher t\n" +
                    "            JOIN class c ON t.id = c.teacher_id\n" +
                    "            WHERE t.name='王克晶'";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String name = rs.getString("t.name");
                String className = rs.getString("c.name");
                int floor = rs.getInt("floor");
                /*
                也支持通过字段下标获取，注意：下标从1开始。
                String name = rs.getString(1);
                String className = rs.getString(2);
                int floor = rs.getInt(3);
                 */
                System.out.println(name+"\t"+className+"\t"+floor);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
